package service

import (
	"gin-vue-admin/global"
	"gin-vue-admin/model"
	"gin-vue-admin/model/request"
)

//@author: [piexlmax](https://github.com/piexlmax)
//@function: CreateSysCaipiaoNumbers
//@description: 创建SysCaipiaoNumbers记录
//@param: sysCaipiaoNumbers model.SysCaipiaoNumbers
//@return: err error

func CreateSysCaipiaoNumbers(sysCaipiaoNumbers model.SysCaipiaoNumbers) (err error) {
	err = global.GVA_DB.Create(&sysCaipiaoNumbers).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: DeleteSysCaipiaoNumbers
//@description: 删除SysCaipiaoNumbers记录
//@param: sysCaipiaoNumbers model.SysCaipiaoNumbers
//@return: err error

func DeleteSysCaipiaoNumbers(sysCaipiaoNumbers model.SysCaipiaoNumbers) (err error) {
	err = global.GVA_DB.Delete(&sysCaipiaoNumbers).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: DeleteSysCaipiaoNumbersByIds
//@description: 批量删除SysCaipiaoNumbers记录
//@param: ids request.IdsReq
//@return: err error

func DeleteSysCaipiaoNumbersByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]model.SysCaipiaoNumbers{}, "id in ?", ids.Ids).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: UpdateSysCaipiaoNumbers
//@description: 更新SysCaipiaoNumbers记录
//@param: sysCaipiaoNumbers *model.SysCaipiaoNumbers
//@return: err error

func UpdateSysCaipiaoNumbers(sysCaipiaoNumbers model.SysCaipiaoNumbers) (err error) {
	err = global.GVA_DB.Save(&sysCaipiaoNumbers).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetSysCaipiaoNumbers
//@description: 根据id获取SysCaipiaoNumbers记录
//@param: id uint
//@return: err error, sysCaipiaoNumbers model.SysCaipiaoNumbers

func GetSysCaipiaoNumbers(id uint) (err error, sysCaipiaoNumbers model.SysCaipiaoNumbers) {
	err = global.GVA_DB.Where("id = ?", id).First(&sysCaipiaoNumbers).Error
	return
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetSysCaipiaoNumbersInfoList
//@description: 分页获取SysCaipiaoNumbers记录
//@param: info request.SysCaipiaoNumbersSearch
//@return: err error, list interface{}, total int64

func GetSysCaipiaoNumbersInfoList(info request.SysCaipiaoNumbersSearch) (err error, list interface{}, total int64) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&model.SysCaipiaoNumbers{})
	var sysCaipiaoNumberss []model.SysCaipiaoNumbers

	//如果有开始时间和结束时间，则预先搜索出开始时间和结束
	if info.Starttime != "" {
		//去检索开始时间和结束时间
		starttime := model.SysCaipiaoNumbers{}
		endtime := model.SysCaipiaoNumbers{}
		err = global.GVA_DB.Model(model.SysCaipiaoNumbers{}).Debug().Where("time = ?", info.Starttime).First(&starttime).Error
		err = global.GVA_DB.Model(model.SysCaipiaoNumbers{}).Debug().Where("time = ?", info.Endtime).First(&endtime).Error
		db = db.Where("`id` >=  ? and id <=?", starttime.ID,endtime.ID)
		//#fmt.Print(starttime.ID,endtime.ID)
	}

	// 如果有条件搜索 下方会自动创建搜索语句
	if info.ProjectId != 0 {
		db = db.Where("`project_id` = ?", info.ProjectId)
	}
	err = db.Count(&total).Error
	err = db.Limit(limit).Offset(offset).Find(&sysCaipiaoNumberss).Error
	return err, sysCaipiaoNumberss, total
}

func GetSysCaipiaoNumberslengreInfo(info request.SysCaipiaoNumberslengreData) (err error, list interface{}, total int64) {
	var idmin int
	//查询指定期数内的id值
	sqlidmin := `
SELECT
min(t.id) as idmin
FROM
	( SELECT id FROM sys_caipiao_numbers WHERE project_id = ? ORDER BY id DESC LIMIT ? ) as t;

`
	err = global.GVA_DB.Raw(sqlidmin, info.Project_id, info.Qishu).Scan(&idmin).Error
	//根据指定内值统计对应的数字出现次数
	sql := `
select * from (

SELECT
	t.number5 as number,
	count( t.number5 ) as count
FROM
	(
	( SELECT number5, id, project_id FROM sys_caipiao_numbers ) UNION ALL
	( SELECT number4, id, project_id FROM sys_caipiao_numbers ) UNION ALL
	( SELECT number3, id, project_id FROM sys_caipiao_numbers ) UNION ALL
	( SELECT number2, id, project_id FROM sys_caipiao_numbers ) UNION ALL
	( SELECT number1, id, project_id FROM sys_caipiao_numbers ) 
	) AS t 
WHERE
	t.project_id = ? 
	AND t.id >= ? 
GROUP BY
	t.number5


) as a ORDER BY a.count desc;`
	var result []request.NumberslengreResult
	err = global.GVA_DB.Raw(sql, info.Project_id, idmin).Scan(&result).Error

	return err, result, total
}
